﻿using lib;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.Sockets;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DocClient
{
    public partial class inlogScherm : Form
    {
        public inlogScherm()
        {
            InitializeComponent();
            IPTextbox.Text = "127.0.0.1";
        }

        private void loginButton_Click(object sender, EventArgs e)
        {
            string name = NameTextbox.Text;
            string password = PasswordTextbox.Text;
            string IP = IPTextbox.Text;
            if (NameTextbox.Text == "" && PasswordTextbox.Text == "")
            {
                MessageBox.Show("username and password are empty");
            }

            else if (NameTextbox.Text == "")
            {
                MessageBox.Show("username is empty");
            }


            else if (PasswordTextbox.Text == "")
            {
                MessageBox.Show("password is empty");
            }

            try
            {
                TcpClient serverConnection = new TcpClient(IP, 60530);
                BinaryFormatter binaryFormatter = new BinaryFormatter();
                SecureConnectionClient secureConnection = new SecureConnectionClient(serverConnection);
                //binaryFormatter.Serialize(serverConnection.GetStream(), "Arts:" + name + ":" + password);
                //string connectionStatus = (binaryFormatter.Deserialize(serverConnection.GetStream())) as String;
                secureConnection.SendObject("Arts:" + name + ":" + password);
                string connectionStatus = secureConnection.ReadObjectAsString();


                if (connectionStatus == "connection accepted")
                {
                    new DoctorForm(serverConnection, secureConnection).Show();
                    this.Hide();
                }
                else
                {
                    MessageBox.Show("username or password is invalid");
                }
            }
            catch (Exception)
            {
                MessageBox.Show("invalid IP adress");
            }

           
        }

        private void nameLabel_Click(object sender, EventArgs e)
        {

        }
    }
}
